package com.wyh.utils.feemanagementutils;

import java.security.SecureRandom;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class RandomNumberGenerator {

    private static final Random RANDOM = new Random();
    private static final int MAX_RANDOM_PART = 1000; // 可以生成三位数 (0-999)
    private static final int TIMESTAMP_PART_LENGTH = 6; // 使用时间戳的后6位
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static final int RANDOM_STRING_LENGTH = 8;
    private static final Random RANDOMS = new SecureRandom();

    /**
     * Generates a random string based on the current timestamp and random characters.
     *
     * @return a unique random string
     */
    public static String generateRandomString() {
        // Get the current timestamp in milliseconds
        long timestamp = System.currentTimeMillis();

        // Format the timestamp as a date-time string
        String timestampStr = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date(timestamp));

        // Generate a random string of defined length
        StringBuilder randomString = new StringBuilder(RANDOM_STRING_LENGTH);
        for (int i = 0; i < RANDOM_STRING_LENGTH; i++) {
            int index = RANDOMS.nextInt(CHARACTERS.length());
            randomString.append(CHARACTERS.charAt(index));
        }

        // Combine the timestamp and the random string
        return timestampStr + randomString.toString();
    }

    public static int generateRandomKey() {
        long currentTimeMillis = System.currentTimeMillis();
        String timestampPart = String.valueOf(currentTimeMillis).substring(7); // 取时间戳的后6位
        int randomPart = RANDOM.nextInt(MAX_RANDOM_PART); // 生成0-999的随机数
        String keyString = timestampPart + String.format("%03d", randomPart); // 拼接成9位字符串
        return Integer.parseInt(keyString); // 转换为整数
    }
}